//
//  LLBWithdrawalMessageCell.m
//  FLLTB
//
//  Created by apple on 2019/7/8.
//  Copyright © 2019 LLB. All rights reserved.
//
#define Test_Message_Font_Size 16

#import "LLBWithdrawalMessageCell.h"

@implementation LLBWithdrawalMessageCell
+ (CGSize)sizeForMessageModel:(RCMessageModel *)model
      withCollectionViewWidth:(CGFloat)collectionViewWidth
         referenceExtraHeight:(CGFloat)extraHeight {
    LLBWithdrawalMessage *message = (LLBWithdrawalMessage *)model.content;
//    CGSize size = [LLBWithdrawalMessageCell getBubbleBackgroundViewSize:message];
    
//    CGFloat __messagecontentview_height = size.height;
    CGFloat __messagecontentview_height = 140;
    __messagecontentview_height += extraHeight;
    
//    return CGSizeMake(kScreenWidth, 120);
    
    return CGSizeMake(collectionViewWidth, __messagecontentview_height);
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initialize];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self initialize];
    }
    return self;
}

- (void)initialize {
    //图
        self.leftImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 50, 50)];
        self.leftImageView.backgroundColor = [UIColor  whiteColor];
        [self.messageContentView addSubview:self.leftImageView];
        
        self.textLabel = [[UILabel alloc] initWithFrame:CGRectMake(70, 11, 120, 20)];
        [self.textLabel setFont:[UIFont systemFontOfSize:15]];
        self.textLabel.text = @"姓名";
        
        [self.textLabel setLineBreakMode:NSLineBreakByWordWrapping];
        [self.textLabel setTextAlignment:NSTextAlignmentLeft];
    //    [self.textLabel setTextColor:[UIColor redColor]];
        [self.messageContentView addSubview:self.textLabel];
        
        
        self.messageContentView.backgroundColor = [UIColor whiteColor];
        self.messageContentView.layer.masksToBounds = YES;
        self.messageContentView.layer.cornerRadius = 10;

        // 提示
        self.couponLabel = [[UILabel alloc]initWithFrame:CGRectMake(70, 40, 120, 20)];
        self.couponLabel.font = [UIFont systemFontOfSize:13];
        self.couponLabel.textColor = [UIColor blackColor];
        self.couponLabel.text = @"发出了提现申请";
        [self.messageContentView addSubview:self.couponLabel];
        
        // 提现金额：
        self.priceLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 70, 72, 20)];
        self.priceLabel.font = [UIFont systemFontOfSize:14];
        self.priceLabel.textColor = [UIColor blackColor];
        self.priceLabel.text = @"提现金额：";
        [self.messageContentView addSubview:self.priceLabel];
        
        // 额度
        self.originalPriceLabel = [[UILabel alloc]initWithFrame:CGRectMake(80, 70, 100, 20)];
        self.originalPriceLabel.font = [UIFont systemFontOfSize:16];
        self.originalPriceLabel.textColor = [UIColor colorWithHexString:@"#00C8AC"];
        self.originalPriceLabel.text = @"0.00";
        [self.messageContentView addSubview:self.originalPriceLabel];
        // 提现账号
        self.reurnMoneyLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 100, 72, 20)];
        self.reurnMoneyLabel.font = [UIFont systemFontOfSize:14];
        self.reurnMoneyLabel.textColor = [UIColor blackColor];
        self.reurnMoneyLabel.text = @"提现账号：";
        [self.messageContentView addSubview:self.reurnMoneyLabel];
        
        // 账号
        self.MoneyLabel = [[UILabel alloc]initWithFrame:CGRectMake(80, 100, 120, 20)];
        self.MoneyLabel.font = [UIFont systemFontOfSize:16];
        self.MoneyLabel.textColor = [UIColor colorWithHexString:@"#00C8AC"];
        self.MoneyLabel.text = @"137****521";
        [self.messageContentView addSubview:self.MoneyLabel];
        
        
        self.bubbleBackgroundView.userInteractionEnabled = YES;
        UILongPressGestureRecognizer *longPress =
        [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressed:)];
        [self.bubbleBackgroundView addGestureRecognizer:longPress];
        
        UITapGestureRecognizer *textMessageTap =
        [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTextMessage:)];
        textMessageTap.numberOfTapsRequired = 1;
        textMessageTap.numberOfTouchesRequired = 1;
        [self.messageContentView addGestureRecognizer:textMessageTap];
        self.messageContentView.userInteractionEnabled = YES;
}

- (void)tapTextMessage:(UIGestureRecognizer *)gestureRecognizer {
    if ([self.delegate respondsToSelector:@selector(didTapMessageCell:)]) {
        [self.delegate didTapMessageCell:self.model];
    }
}

- (void)setDataModel:(RCMessageModel *)model {
    [super setDataModel:model];
    
    [self setAutoLayout];
}

- (void)setAutoLayout {
    LLBWithdrawalMessage *testMessage = (LLBWithdrawalMessage *)self.model.content;
    if (testMessage) {
//        self.textLabel.text = testMessage.money;
    }
    
//    CGSize textLabelSize = [[self class] getTextLabelSize:testMessage];
//    CGSize bubbleBackgroundViewSize = [[self class] getBubbleSize:textLabelSize];
    CGSize bubbleBackgroundViewSize = CGSizeMake(kScreenWidth/2, 80);
    
    CGRect messageContentViewRect = self.messageContentView.frame;
    //拉伸图片
    if (MessageDirection_RECEIVE == self.messageDirection) {
        self.textLabel.text =[NSString stringWithFormat:@"%@",testMessage.content[@"name"]];
        self.originalPriceLabel.text =[NSString stringWithFormat:@"%@",testMessage.content[@"money"]];
        self.MoneyLabel.text =[NSString stringWithFormat:@"%@",testMessage.content[@"id"]];
    } else {
        self.textLabel.text =[NSString stringWithFormat:@"%@",testMessage.content[@"name"]];
        self.originalPriceLabel.text =[NSString stringWithFormat:@"%@",testMessage.content[@"money"]];
        self.MoneyLabel.text =[NSString stringWithFormat:@"%@",testMessage.content[@"id"]];
    }
}


//长按
- (void)longPressed:(id)sender {
    UILongPressGestureRecognizer *press = (UILongPressGestureRecognizer *)sender;
    if (press.state == UIGestureRecognizerStateEnded) {
        return;
    } else if (press.state == UIGestureRecognizerStateBegan) {
        [self.delegate didLongTouchMessageCell:self.model inView:self.bubbleBackgroundView];
    }
}

//+ (CGSize)getTextLabelSize:(LLBWithdrawalMessage *)message {
//    if ([message.money length] > 0) {
//        float maxWidth = kScreenWidth -
//        (10 + [RCIM sharedRCIM].globalMessagePortraitSize.width + 10) * 2 - 5 - 35;
//        CGRect textRect = [message.money
//                           boundingRectWithSize:CGSizeMake(maxWidth, 8000)
//                           options:(NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin |
//                                    NSStringDrawingUsesFontLeading)
//                           attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:Test_Message_Font_Size]}
//                           context:nil];
//        textRect.size.height = ceilf(textRect.size.height);
//        textRect.size.width = ceilf(textRect.size.width);
//        return CGSizeMake(textRect.size.width + 5, textRect.size.height + 5);
//    } else {
//        return CGSizeZero;
//    }
//}

+ (CGSize)getBubbleSize:(CGSize)textLabelSize {
    CGSize bubbleSize = CGSizeMake(textLabelSize.width, textLabelSize.height);
    
    if (bubbleSize.width + 12 + 20 > 50) {
        bubbleSize.width = bubbleSize.width + 12 + 20;
    } else {
        bubbleSize.width = 50;
    }
    if (bubbleSize.height + 7 + 7 > 40) {
        bubbleSize.height = bubbleSize.height + 7 + 7;
    } else {
        bubbleSize.height = 40;
    }
    
    return bubbleSize;
}

//+ (CGSize)getBubbleBackgroundViewSize:(LLBWithdrawalMessage *)message {
//    CGSize textLabelSize = [[self class] getTextLabelSize:message];
//    return [[self class] getBubbleSize:textLabelSize];
//}
@end
